package cn.com.idmy.base.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import org.dromara.hutool.core.convert.ConvertUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;

import static java.lang.System.getProperty;

public class CfgUtil {
    public static String prefix = "dmy.cfg.";

    public static @NotNull List<String> listString(@NotNull String key, @NotNull List<String> defaultValue) {
        var ls = JSON.parseObject(getProperty(prefix + key), new TypeReference<List<String>>() {
        });
        return Objects.requireNonNullElse(ls, defaultValue);
    }

    public static @NotNull List<Long> listLong(@NotNull String key, @NotNull List<Long> defaultValue) {
        var ls = JSON.parseObject(getProperty(prefix + key), new TypeReference<List<Long>>() {
        });
        return Objects.requireNonNullElse(ls, defaultValue);
    }

    public static @NotNull String getString(@NotNull String key, @NotNull String defaultValue) {
        return getProperty(prefix + key, defaultValue);
    }

    public static @Nullable String getString(@NotNull String key) {
        return getProperty(prefix + key);
    }

    public static int getInt(@NotNull String key, int defaultValue) {
        return ConvertUtil.toInt(getProperty(prefix + key), defaultValue);
    }

    public static @Nullable Integer getInteger(@NotNull String key) {
        return ConvertUtil.toInt(getProperty(prefix + key));
    }

    public static long getLong(@NotNull String key, long defaultValue) {
        return ConvertUtil.toLong(getProperty(prefix + key), defaultValue);
    }

    public static @Nullable Long getLong(@NotNull String key) {
        return ConvertUtil.toLong(getProperty(prefix + key));
    }

    public static boolean getBoolean(@NotNull String key, boolean defaultValue) {
        return ConvertUtil.toBoolean(getProperty(prefix + key), defaultValue);
    }

    public static boolean getBoolean(@NotNull String key) {
        var bol = ConvertUtil.toBoolean(getProperty(prefix + key));
        return bol != null && bol;
    }
}
